package com.godme.rabbitmq.config;


import com.godme.rabbitmq.props.RabbitProperties;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {


    @Autowired
    RabbitProperties rabbitProperties;

    @Bean("orderQueue")
    public Queue orderQueue(){
        Map<String,Object> arguments = new HashMap<>(2);
        // 绑定该队列到私信交换机
        arguments.put("x-dead-letter-exchange",rabbitProperties.getDlxExchange());
        arguments.put("x-dead-letter-routing-key",rabbitProperties.getDlxRoutingKey());
        return QueueBuilder
                .durable(rabbitProperties.getOrderQueue())
                .withArguments(arguments)
                .build();
    }


    @Bean("orderExchange")
    public Exchange orderExchange(){
        return ExchangeBuilder
                .directExchange(rabbitProperties.getOrderExchange())
                .durable(true)
                .build();
    }


    @Bean("orderBinding")
    public Binding orderBinding(@Qualifier("orderQueue") Queue orderQueue, @Qualifier("orderExchange") Exchange orderExchange){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderExchange)
                .with(rabbitProperties.getOrderRoutingKey())
                .noargs();
    }


    @Bean("dlxExchange")
    public Exchange dlxExchange(){
        return ExchangeBuilder
                .directExchange(rabbitProperties.getDlxExchange())
                .build();
    }

    @Bean("dlxQueue")
    public Queue dlxQueue(){
        return QueueBuilder
                .durable(rabbitProperties.getDlxQueue())
                .build();
    }

    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxQueue") Queue dlxQueue, @Qualifier("dlxExchange") Exchange dlxExchange){
        return BindingBuilder
                .bind(dlxQueue)
                .to(dlxExchange)
                .with(rabbitProperties.getDlxRoutingKey())
                .noargs();
    }







}
